<?php

namespace App\Http\Controllers\UserCenter;

use App\Eloquent\User;
use App\Eloquent\Projects;
use App\Admin\Eloquent\GiftActivity;
use App\Admin\Eloquent\ProjectProfit;
use App\Eloquent\TradeRecords;
use App\Http\Controllers\Controller;
use App\Extensions\Pagination\BoosstrapPageUser;
use App\Extensions\Pagination\BoosstrapPageProject;
use DB;
use Log;
use Illuminate\Http\Request;
use CreateSingleHostingPayTrade;
use CreateHostingCollectTrade;
/**
 * 融资人
 */
class LoanerController extends Controller{

	public function init(){
		$user = User::user();
		$user->checkRole('Loaner');	
	}
	/**
	 * 融资人总览
	 * 
	 * @return [type] [description]
	 */
	public function index(){
		$user=User::user();
  	$sina_balance=$user->queryBalance();//新浪余额
  	$projects=collect(Projects::where('financier_id',$user->id)->whereNotIn('status',['none','down','stop','check'])->get());
  	$project_id=$projects->pluck('id');
  	$profit=collect(ProjectProfit::whereIn('project_id',$project_id)->get());

    $list=Projects::whereNotIn('status',['none','down','stop','check'])->where('financier_id',$user->id)->orderBy('created_at','desc')->paginate(8);
		$links = (new BoosstrapPageUser($list))->render();

		return view('web.loaner.index',compact('user','sina_balance','projects','list','links','profit'));
	}
	
	/**
	 * 待还款
	 * 
	 * @return [type] [description]
	 */
	public function pendingRepayment(Request $q){
		$find=$q->all();
		$user=User::user();
		$list=ProjectProfit::join('projects','project_profits.project_id','=','projects.id')
			->join('codes','codes.code','=','projects.repayment')
			->select('project_profits.*','projects.projects_name as projects_name','codes.name as repayment_name',DB::raw('sum(project_profits.money) as money'),DB::raw('sum(project_profits.profit) as profit'))
			->where('project_profits.type',1)
			->where('projects.financier_id',$user->id);

		if(!isset($find['status'])){
			$find['status']=$q->session()->get('repayment_status', 0);
		}
		$q->session()->put('repayment_status',$find['status']);
		$list=$list->where('project_profits.status',$find['status']);		
		if(isset($find['last_month']) && !empty($find['last_month'])){
			$end_time=time();
			$begin_time=strtotime('-'.$find['last_month'].' month',$end_time);

			$find['begin_time']=date('Y-m-d',$begin_time);
			$find['end_time']=date('Y-m-d',$end_time);
		}
		if(isset($find['begin_time']) && !empty($find['begin_time'])){
			$list=$list->where('project_profits.input_time','>=',$find['begin_time']);
		}
		if(isset($find['end_time']) && !empty($find['end_time'])){
			$list=$list->where('project_profits.input_time','<=',$find['end_time']);
		}

		$list=$list->groupBy('project_profits.project_id')->groupBy('project_profits.input_time')
			->orderBy('project_profits.input_time','asc')->orderBy('project_profits.id','desc')->paginate(8);
		$links = (new BoosstrapPageUser($list))->render();

		return view('web.loaner.pending_repayment',compact('list','links','find'));
	}

	/**
	 * 还款操作
	 */
	public function repayment($id){
		$user=User::user();
		$project_profit=ProjectProfit::find($id);
		$list=ProjectProfit::join('projects','projects.id','=','project_profits.project_id')
			->where('projects.financier_id',$user->id)
			->where('project_profits.input_time',$project_profit->input_time)
			->where('project_profits.project_id',$project_profit->project_id)->get();

		$project=Projects::where('id',$project_profit->project_id)->where('status','payment')->where('financier_id',$user->id)->first();
		if(empty($project)){abort(600,'未找到融资项目记录');}

		//代收
		$money=$list->sum('money')+$list->sum('profit');

		$remarks='代收还款金';
		$sn=createSn();
    $data=[
      'out_trade_no'=>$sn,
      'out_trade_code'=>'1002',
      'summary'=>$remarks,
      'payer_id'=>$user->identity_id,
      'payer_identity_type'=>'UID',
      'pay_method'=>'online_bank^'.$money.'^SINAPAY,DEBIT,C',
      'return_url'=>config('sinapay.notify_url').'/loaner/pending_repayment',
      'notify_url'=>config('sinapay.notify_url').'/return/payment'
    ];
    //添加还款流水记录
    TradeRecords::insert(['type'=>3,'project_id'=>$project->id,'sn'=>$sn,'pay_user'=>$user->id,
    	'money'=>$money,'remarks'=>$remarks,'created_at'=>date('Y-m-d H:i:s'),'gift'=>$project_profit->id
    ]);
    
    return CreateHostingCollectTrade::send($data);
	}

	/**
	 * 已还款
	 * 
	 * @return [type] [description]
	 */
	public function alreadyRepayment(){
		//TODO
		return view('web.loaner.already_repayment');
	}

	/**
	 * 融资管理
	 * 
	 * @return [type] [description]
	 */
	public function financingManagement(){
		$user=User::user();
  	$sina_balance=$user->queryBalance();//新浪余额
  	$projects=collect(Projects::where('financier_id',$user->id)->get());

    $list=Projects::where('status','<>','none')->where('financier_id',$user->id)->where('status','<>','')->orderBy('created_at','desc')->paginate(8);
		$links = (new BoosstrapPageUser($list))->render();

		return view('web.loaner.index',compact('user','sina_balance','projects','list','links'));
	}
}